/**
 *
 * 一个封装了鼠标事件的类 mouseEvent.js
 *
 * Daniel
 *
 * 2018.05.16
 *
 * **/

var mouseEvents = function(){
    
        this.domElement = document;
        this.enable = true;                     //控制整个鼠标事件
        this.mouseObject = {
            winX:null,
            winY:null
        };                                  //鼠标监听值
        
        var scope = this;
        
        //移除所有事件
        this.dispose = function () {
            scope.domElement.removeEventListener("mousedown",down,false);
            scope.domElement.removeEventListener("mousemove",move,false);
            scope.domElement.removeEventListener("mouseup",up,false);
        };
        
        //恢复所有事件
        this.enabledPose = function(){
            scope.domElement.addEventListener("mousedown",down,false);
            scope.domElement.addEventListener("mousemove",move,false);
            scope.domElement.addEventListener("mouseup",up,false);
        };
        
        //鼠标监听器
        this.monitor =function(event,object){
        var flag = event.button;
        var wx = event.clientX;
        var wy = event.clientY;
        var wFx = object.winX;
        var wFy = object.winY;
        var offset = false;
        var eventFlag = wFx;
        if(eventFlag == null){
            object.winX = wx;
            object.winY = wy;
            return;
        }else{
            if( wFx == wx &&  wFy == wy)offset = true;
            object.winX = null;
            object.winY = null;
        }
        return{
            flag: flag,
            offset: offset
        }
        
    };
        
        //初始化
        this.init= function (parameters) {
            this.dispose();
            this.enable = parameters.enable|| true;
            this.domElement = parameters.domElement|| document;
            this.enabledPose();
        
        };
    
    
    this.down = {
        set: function(fun){
            var callback = fun;
            this.get = function(event){
                var object = scope.monitor(event,scope.mouseObject);
                callback(event,object);
            }
        },
        get: function(){
        
        },
        enable: true
    };
    
   this.move = {
        set: function(fun){
            var callback = fun;
            this.get = function(event){
                callback(event)
            }
        },
        get: function(){
        
        },
        enable: true
    };
    
   this.up = {
        set: function(fun){
            var callback = fun;
            this.get = function(event){
                var object = scope.monitor(event,scope.mouseObject);
                callback(event,object);
            }
        },
        get: function(){
        
        },
        enable: true
    };
    
    
    
    
    //鼠标摁下事件
        function down(event){
            if(scope.enable == false)return;
            if(scope.down.enable == false || event.button !== 0)return;
            event.preventDefault();
            scope.down.get(event);
        }
    
    //鼠标移动事件
        function move(event){
            if(scope.enable == false)return;
            if(scope.move.enable == false|| event.button !== 0)return;
            event.preventDefault();
            scope.move.get(event);
            
        }
    
    //鼠标抬下事件
        function up(event){
            if(scope.enable == false)return;
            if(scope.up.enable == false|| event.button !== 0)return;
            event.preventDefault();
            scope.up.get(event);
        }
        
    
    
        
        scope.domElement.addEventListener("mousedown",down,false);
        scope.domElement.addEventListener("mousemove",move,false);
        scope.domElement.addEventListener("mouseup",up,false);
        
};

mouseEvents.prototype = Object.create(THREE.EventDispatcher.prototype);
mouseEvents.prototype.constructor = mouseEvents;


